<!--vue模板-->
<template>
  <div>
    <div class="tip">咨询信息</div>
    <el-form class="formData">
      <el-form-item label="联系工程师">
        <el-select v-model="data.saleId">
          <el-option
            v-for="item in saleOptions"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="询价类型">
        <el-radio-group v-model="data.type">
          <el-radio :value="1">询价单</el-radio>
          <el-radio :value="2">咨询单</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <div class="tip">产品信息</div>
    <el-table :data="tableData" style="width: 1000px; margin: auto" border>
      <el-table-column label="产品图片" width="100">
        <template v-slot="{ row }">
          <!-- 检查图片格式是否为 JPG -->
          <template v-if="isJpgImage(row.picurl)">
            <img
              :src="host + row.picurl"
              alt="产品图片"
              style="max-width: 100%; max-height: 100px;"
              @error="handleImageError(row)"
            >
          </template>
          <template v-else>
            <!-- 非 JPG 图片显示提示 -->
            <span>非 JPG 图片</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="产品名称" />
      <el-table-column prop="model" label="产品型号" width="150" />
      <el-table-column prop="price" label="产品价格" width="150" />
      <el-table-column label="需求数量" width="180">
        <template v-slot="{ row }">
          <el-input-number v-model="row.num" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template v-slot="{ row }">
          <el-button @click="del(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="width: 1000px; margin: 20px auto; text-align: right">
      <el-button type="primary" @click="isshow = true">选择产品</el-button>
      <el-button type="primary" @click="save">保存</el-button>
    </div>

    <!--选择产品-->
    <ChooseProduct :visible="isshow" @close="isshow = false" @choose="chg" />
  </div>
</template>

<!--vue 组件选项定义-->
<script setup>
import { ref ,onMounted} from "vue";
import ChooseProduct from "./ChooseProduct.vue";
import { saveInquiry,queryAllSale } from "@/api/customerinquiry.js";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import host from"@/utils/env.js"
//获取路由实例
const router = useRouter();
const data = ref({
  saleId: "",
  type: 1
});
//是否显示选择产品
const isshow = ref(false);
const tableData = ref([]);

// 图片加载错误处理
const handleImageError = (row) => {
  console.log("图片加载失败",host + row.picurl)
  ElMessage.error(`图片加载失败: ${host + row.picurl}`);
  // 可以在这里设置默认图片
  // row.picurl = 'default-image-url';
};

// 检查图片是否为 JPG 格式
const isJpgImage = (url) => {
  const lowerCaseUrl = url.toLowerCase();
  return lowerCaseUrl.endsWith('.jpg') || lowerCaseUrl.endsWith('.jpeg');
};


function del(row) {
  let index = tableData.value.indexOf(row);
  tableData.value.splice(index, 1);
}
//获取销售人员列表
const saleOptions =ref();
//查询联系工程师
async function querySale(){
  const res = await queryAllSale();
  if(res.code == 200){

    saleOptions.value = res.data;
  }
}

//--选择产品--
function chg(rows) {
  rows.forEach(el => {
    el.num = 1;
    tableData.value.push(el);
  });
}

async function save() {
  data.value.customerInquiryDetails = tableData.value;
  let res = await saveInquiry(data.value);
  console.log("保存结果",data.value);
  if (res.code == 200) {
    ElMessage({
      message: "保存成功",
      type: "success"
    });
    await router.push("/customer/inquiry");
  }
}

//选择产品
function choose() {}

//生命周期钩子
onMounted(() => {
  querySale();
});
</script>

<!--vue样式 scoped当前样式只适用于当前组件-->
<style scoped>
.tip {
  width: 1000px;
  margin: auto;
  height: 60px;
  line-height: 60px;
  border: #ccc solid 1px;
  padding-left: 20px;
}

.formData {
  width: 1000px;
  margin: auto;
  background-color: #fff;
  padding: 20px;
}
</style>
